<?php
namespace app\admin\controller;

use think\facade\Config;
use think\Controller;
use think\Db;
use think\facade\Session;

/**
 * 后台登录
 * Class Login
 * @package app\admin\controller
 */
class Login extends Controller
{
    /**
     * 后台登录
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 登录验证
     * @return string
     */
    public function login()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->only(['username', 'password']);
            $validate_result = $this->validate($data, 'Login');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $admin_user = Db::name('admin_user')->field('id,username,password,status')->where('username', $data['username'])->find();
                if (empty($admin_user)) {
                     $this->error('用户名错误');
                }
				if(!password_verify($data['password'],$admin_user['password']))
				{
					$this->error('密码错误');
				}
				if ($admin_user['status'] != 1) {
                    $this->error('当前用户已禁用');
                } else {
                    Session::set('admin_id', $admin_user['id']);
                    Session::set('admin_name', $admin_user['username']);
                    Db::name('admin_user')->update(
                        [
                            'last_login_time' => date('Y-m-d H:i:s', time()),
                            'last_login_ip'   => $this->request->ip(),
                            'id'              => $admin_user['id']
                        ]
                    );
                    $this->success('登录成功', '/admin/home');
                }
            }
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        Session::delete('admin_id');
        Session::delete('admin_name');
        $this->success('退出成功', 'admin/login/index');
    }
}
